

global m n costs cells gi gj gcosti gcostj agents velocities

m = 30;
n = m;
iters = 50;

costs = zeros(m);
cells = zeros(m);
agents = m.*rand(2,n);
velocities = zeros(2,n);
history = zeros(2,iters+1);
gi = zeros(m);
gj = zeros(m);
gcosti = zeros(m);
gcostj = zeros(m);


cost(m/2-3:m/2+4,m/2-3:m/2+2) = 50;

close all;
H = figure();
frames = moviein(iters);

for iter = 1:iters
    
    history(:,iter) = agents(:,1);
    
    [gi, gj] = gradient(cells);
    [gcosti, gcostj] = gradient(cost);

    
    for k = 1:n
        updateagentpath(k);
    end
    
    for i = 2:m-1
        for j = 2:m-1
       
           updatecell(i,j);
        end
    end

    clf;
    hold on;
    axis([1 m 1 m]);
    surf(-cells'-1);
    quiver(gj',gi');
    scatter(agents(1,:),agents(2,:),'r');
    plot(history(1,1:iter), history(2,1:iter),'w');
    hold off;

    frames(:,iter) = getframe(H);
end

movie(H,frames,1,24);
